home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / str.arc / STR.C < prev    next >
C/C++ Source or Header  |  1986-04-18  |  5KB  |  147 lines

  1. /*
  2.  * str - extract the ASCII strings from a binary file
  3.  *
  4.  * This program opens a file and then copies input to stdout, searching for
  5.  * ASCII strings.  A string is "tlen" printable ASCII characters terminated by
  6.  * a NULL, <newline> or <return>, or "slen" characters ended with a
  7.  * non-printing ASCII code.  It pauses output after SCRSIZ lines if output is
  8.  * to the console screen.  The values of "tlen" and "slen" can be set by
  9.  * command-line options.  To compile with DeSmet C define the symbolic constant
  10.  * DESMET; for CI-C86 define the symbolic constant CIC86; for Microsoft C
  11.  * define the symbolic constant MSC.  Your config.sys file must contain
  12.  * "device=ansi.sys".
  13.  *
  14.  * This program was written by R. Edward Nather and is placed in the Public
  15.  * Domain without any restrictions whatsoever.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include "intdefs.h"
  21.  
  22. int slen = 6;       /* default length for unterminated string */
  23. int tlen = 4;       /* default length for terminated string   */
  24.  
  25. int tsc;            /* set true if output is to console screen */
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char *argv[];
  30. {
  31. extern int tsc, toscreen();
  32. int i;
  33. FILE *fp;
  34.  
  35. tsc = toscreen();
  36. while(argc > 1 && argv[1][0] == '-') {
  37.     switch(argv[1][1]) {
  38.         case 'l':           /* -l: set min unterminated string length */
  39.             slen = atoi(&argv[1][2]);
  40.             break;
  41.         case 't':           /* -t: set min terminated string length */
  42.             tlen = atoi(&argv[1][2]);
  43.             break;
  44.         default:
  45.             fprintf(stderr, "str: unknown option \"%s\"\n", argv[1]);
  46.             exit(1);
  47.         }
  48.     argc--;
  49.     argv++;
  50.     }
  51. if(argc < 2) {
  52.     fprintf(stderr, "Use: str [-t##] [-l##] filename [filename...]\n");
  53.     exit(1);
  54.     }
  55. for(i = 1; i < argc; i++)
  56.     if((fp=fopen(argv[i], "rb")) == NULL) {
  57.         fprintf(stderr, "str: can't open \"%s\"\n",argv[i]);
  58.         exit(1);
  59.         } 
  60.     else {
  61.         filecopy(fp);
  62.         fclose(fp);
  63.     }
  64. exit(0);
  65. }
  66.  
  67. #define MAX 81              /* insert newline before 81st char */
  68.     
  69. filecopy(fp)
  70. FILE *fp;
  71. {
  72. int c;
  73. unsigned char hold[MAX];             /* buffer to hold ASCII strings */
  74. int cnt = 0;                /* current string length */
  75. int wf = 0;                 /* 80 characters seen if non-zero */
  76. int i;
  77.  
  78. while((c = getc(fp)) != EOF) {
  79.     if(isprint(c)) {                        /* if char is printable ASCII */
  80.         hold[cnt++] = c;                    /* save it */
  81.         hold[cnt] = 0;                      /* terminate potential string */
  82.         if(cnt >= MAX) {
  83.             fputs(hold, stdout);            /* print a buffer full */
  84.             cnt = 0;
  85.             hold[0] = 0;
  86.             wf = 1;                         /* and remember we did it */
  87.             }
  88.         }
  89.     else {                                          /* if char not printable */
  90.         if(wf || cnt >= slen || (cnt >= tlen &&
  91.             (c == NULL || c == '\r' || c == '\n'))) {   /* if it's a string */
  92.                 fputs(hold, stdout);                            /* print it */
  93.                 if(isprint(c &= '\177')) {      /* some strings end with    */
  94.                     fputc(c, stdout);           /* 8th bit set in last char */
  95.                     }
  96.                 fputc(endlin(), stdout);              /* end the line */
  97.                 }
  98.         cnt = wf = 0;
  99.         }
  100.     }
  101. }
  102.  
  103.  
  104. /* endlin - end a line and watch for screen overflow */
  105.  
  106. #define SCRSIZ 22
  107. static int lc = 0;                                        /* line counter */
  108.  
  109. endlin()
  110. {
  111. extern int tsc;                             /* true if output is to screen */
  112. register int c;
  113.  
  114. if(tsc && ++lc >= SCRSIZ) {        /* pause if output is to console screen */
  115.     fputs("\n\033[7m--More--", stdout);     /* and we've shown a screenful */
  116.     c = GETC;
  117.     fputs("\033[0m\r\033[K ", stdout);
  118.     switch(c) {
  119.         case '\r':                          /* <RETURN> - show 1 more line */
  120.             lc = SCRSIZ - 1;
  121.             break;
  122.         case 'q':                             /* quit with "q" or "ctrl-C" */
  123.         case '\003':
  124.             exit(0);
  125.         default:
  126.             lc = 0;                         /* else show another screenful */
  127.             break;
  128.         }
  129.     return('\b');
  130.     }   
  131. else
  132.     return('\n');
  133. }
  134.  
  135.  
  136. /* toscreen - find out if output is to console screen */
  137.  
  138. toscreen()
  139. {
  140. STRUCTURE
  141.  
  142. RAX = 0x4400;
  143. RBX = 1;
  144. CALLDOS;
  145. return((RDX & 1) && (RDX & 0x80));                      /* isdev && iscin */
  146. }
  147.